/*
 * @Author: szx
 * @Date: 2022-03-16 16:10:47
 * @LastEditTime: 2022-03-16 16:17:39
 * @Description:
 * @FilePath: \learn\手撸JS源码\new.js
 */
function myNew(constructor, ...args) {
    if (typeof constructor !== 'function') throw `${constructor} is not a constructor`;
    const obj = Object.create(constructor.prototype); // 1/2、创建一个新的对象，并继承其原型
    const res = constructor.apply(obj, args); // 3、执行上下文
    if (res && typeof res === 'object') return res;
    if (res && typeof res === 'function') return res;
    return obj;
}
